Ext.onReady(function(){
	Ext.BLANK_IMAGE_URL = '/js/extjs/resources/images/default/s.gif';
	Ext.form.Field.prototype.msgTarget = 'under';
	Ext.LoadMask.prototype.msg = "Cargando ...";
	Ext.MessageBox.buttonText.yes = "Si";
	Ext.MessageBox.buttonText.no = "No";
	Ext.QuickTips.init();
	var generateRiskTree = function (tree, url) {
		$.ajax({
			type: 'get',
			url: url,
			async: false,
			complete: function(xml) { 
				var doc = xml.responseXML;
				Ext.each(Ext.DomQuery.select('/risks/risk', doc), function(node, index, all){
					var id = Ext.DomQuery.selectValue('id', node);
					var url_node = node.attributes.getNamedItem('href').value;
					var name = Ext.DomQuery.selectValue('name', node);
					tree.addRisk({
						url:url_node,
						text: name
						}, false, true);
				}, this);
			}
		});	
	};

	var mainPanel = new MainPanel();
	var risks = new RiskPanel(mainPanel);
	var ge = new Ext.tree.TreeEditor(risks, {
						allowBlank:false,        
						blankText:'Un nombre es requerido',
						selectOnFocus:true
					});
				
	var viewport = new Ext.Viewport({
			layout: 'border',
			items: [
				new Ext.BoxComponent({ 
					region:'north',
					el: 'header',
					height:50					
				}),
				risks,
				mainPanel
			]	
	});
		
	generateRiskTree(risks, '/risks');
	
	risks.on('append', function (cont, node, n) {
							setTimeout(function(){
									var aux = cont.getNodeById('null');
									ge.editNode = aux;
									ge.startEdit(aux.ui.textNode);
								}, 10);
						}
	);
	risks.on('click', function(node, e) {mainPanel.openTab(node);} );
	ge.on('complete', function(editor, v, sv) {
		//alert(v);alert(sv);
		//TODO: Hacer la llamada ajax para crear el Riesgo. Pregunta: Recargar el arbol para que tenga el id otrogado por la BD?
		
		var xmlDocument = '<?xml version="1.0" encoding="UTF-8" ?><risk><name>';
		xmlDocument += v;
		xmlDocument += '</name><description></description><probability></probability><impact></impact></risk>';
		var id_node = editor.tree.getSelectionModel().getSelectedNode().id;
		var url_risk = '/risks';
		var method = 'post';
		var data = {xml: xmlDocument};
		if (id_node != 'null') {
			url_risk = id_node;
			method = 'put';
			xmlDocument = '<?xml version="1.0" encoding="UTF-8" ?><risk><name>';
			xmlDocument += v;
			xmlDocument += '</name></risk>';
			data = xmlDocument;
		}
		//alert(url_risk);
		//alert(xmlDocument);
		$.ajax({
		  type: method,
		  url: url_risk,
		  dataType: "xml",
		  async: false,
		  data: data,
		  complete: function(xml) {
				if(xml.status != 201 && xml.status != 200){
				//TODO: Si no se puede editar se cambia el texto editado. Si no se puedo agregar, se deberia eliminar el nodo.
					//alert(xml.status);
					Ext.MessageBox.alert('Error', 'No se pudo crear o editar el riesgo');
				} else {
					//alert(xml.getResponseHeader('Location'));		
					if(xml.getResponseHeader('Location') != null){
						editor.tree.getSelectionModel().getSelectedNode().id = xml.getResponseHeader('Location');
					}
					reloadResumen();
					var pg = Ext.getCmp('prop_' + id_node);
					var tp = Ext.getCmp('tab_' + id_node);
					if (pg != null && tp != null) {
						setPropertyGridValue(pg, '1. Nombre', v);
						tp.setTitle(v);
					}
				}
		  }
		});
		
	});	
});
